// import md5 from ;
let md5 = require("js-md5");
let sign = "zegopay-70a7de3d-4aaa-46ce-8d3b-20181213-sign_params-181213";

let common = {
  animate: function(el, target, width) {},
  compileStr(code) {
    //对字符串进行加密
    var c = String.fromCharCode(code.charCodeAt(0) + code.length);
    for (var i = 1; i < code.length; i++) {
      c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1));
    }
    return escape(c);
  },
  //字符串进行解密
  uncompileStr(code) {
    code = unescape(code);
    var c = String.fromCharCode(code.charCodeAt(0) - code.length);
    for (var i = 1; i < code.length; i++) {
      c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1));
    }
    return c;
  },

  formdata1(params) {
    // console.log(Utility);
    let formData = new FormData();
    let signs = md5((params.fuid || params.userId) + sign);

    // console.log(md5("6666"))
    for (let key in params) {
      // console.log(key + '---' + params[key])

      formData.append(key, params[key]);
    }

    formData.append("sign", signs);

    return formData;
  },
  formdata(params) {
    // console.log("params", params);
    // console.log(Utility);
    let formData = new FormData();
    // let signs = md5((params.fuid || params.userId) + sign);

    // console.log(md5("6666"))
    let keyArr = [];
    for (let key in params) {
      // console.log(key + '---' + params[key])
      formData.append(key, params[key]);

      // let keyArr = [];
      keyArr.push(key);
    }
    let reaKryArr = keyArr.sort();

    let newArrStr = "";
    for (let i = 0; i < keyArr.length; i++) {
      newArrStr += reaKryArr[i] + "=" + params[reaKryArr[i]] + "&";
    }
    var str = newArrStr.substr(0, newArrStr.length - 1);

    // console.log("str", str);

    formData.append("sign", md5(str + sign));

    return formData;
  },
  toast: function(params) {
    var el = document.createElement("div");
    el.setAttribute("id", "toast");
    el.innerHTML = params.message;
    document.body.appendChild(el);
    el.classList.add("fadeIn");
    setTimeout(function() {
      el.classList.remove("fadeIn");
      el.classList.add("fadeOut");
      el.addEventListener("animationend", function() {
        el.classList.add("hide");
        el.parentElement.removeChild(el);
      });
    }, params.time);
  },
  loading: function() {
    var el = document.createElement("div");
    el.setAttribute("id", "loading");
    el.classList.add("sk-circle");
    el.innerHTML =
      '<div class="sk-circle1 sk-child"></div>' +
      '<div class="sk-circle2 sk-child"></div>' +
      '<div class="sk-circle3 sk-child"></div>' +
      '<div class="sk-circle4 sk-child"></div>' +
      '<div class="sk-circle5 sk-child"></div>' +
      '<div class="sk-circle6 sk-child"></div>' +
      '<div class="sk-circle7 sk-child"></div>' +
      '<div class="sk-circle8 sk-child"></div>' +
      '<div class="sk-circle9 sk-child"></div>' +
      '<div class="sk-circle10 sk-child"></div>' +
      '<div class="sk-circle11 sk-child"></div>' +
      '<div class="sk-circle12 sk-child"></div>';

    document.body.appendChild(el);
  },
  hide: function() {
    document
      .getElementById("loading")
      .parentElement.removeChild(document.getElementById("loading"));
  }
};

module.exports = common;
